/**
 * Free software.
 */

package org.nanhill.xsheet;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import org.nanhill.xsheet.input.InputBuilder;
import org.nanhill.xsheet.input.excel.DefaultExcelInputBuilder;
import org.nanhill.xsheet.input.excel.ExcelInputBuilder;
import org.nanhill.xsheet.input.xhtml.DefaultXhtmlInputBuilder;
import org.nanhill.xsheet.input.xhtml.XhtmlInputBuilder;
import org.nanhill.xsheet.model.XSheetModel;
import org.nanhill.xsheet.output.OutputBuilder;
import org.nanhill.xsheet.output.OutputDirector;
import org.nanhill.xsheet.output.excel.DefaultExcelOutputBuilder;
import org.nanhill.xsheet.output.excel.ExcelOutputBuilder;
import org.nanhill.xsheet.output.excel.PushCellExcelOutputDirector;
import org.nanhill.xsheet.output.xhtml.DefaultXhtmlOutputBuilder;
import org.nanhill.xsheet.output.xhtml.DefaultXhtmlOutputDirector;
import org.nanhill.xsheet.output.xhtml.XhtmlOutputBuilder;

/**
 * @author alin [xalinx at gmail dot com]
 * @date 2006-12-1
 */
public class SheetTransfer {

	private final static Log logger = LogFactory.getLog(SheetTransfer.class);

	/**
	 * @param args
	 * @throws XSheetParseException
	 * @throws IOException
	 */
	public static void main(String[] args) {
		final String inFile = args[0];
		final String outFile = args[1];

		// -- create input
		InputBuilder input = null;
		String inFileLow = inFile.toLowerCase();
		if (inFileLow.endsWith(".html") || inFileLow.endsWith(".htm")) {
			XhtmlInputBuilder xhtmlInput = new DefaultXhtmlInputBuilder();
			input = xhtmlInput;
			Document doc = null;
			try {
				doc = new SAXReader().read(inFile);
			} catch (DocumentException de) {
				logger.error("Xhtml format incorrect, please check.\n"
						+ de.getMessage());
				System.exit(0);
			}
			xhtmlInput.setDocument(doc);
		} else if (inFileLow.endsWith(".xls")) {
			ExcelInputBuilder excelInput = new DefaultExcelInputBuilder();
			input = excelInput;
			HSSFWorkbook wb = null;
			try {
				POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(
						inFile));
				wb = new HSSFWorkbook(fs);
			} catch (IOException e) {
				logger.error("Occur IO error when reading input excel file .\n"
						+ e.getMessage());
				System.exit(1);
			}
			excelInput.setWorkbook(wb);
		} else {
			logger.info("Unsupport input sheet file type.\n");
			System.exit(0);
		}

		// -- build sheet model
		try {
			input.construct();
		} catch (XSheetParseException e) {
			logger
					.error("Construct sheet model error, please check input file.\n"
							+ e.getMessage());
			System.exit(0);
		}
		XSheetModel model = input.getProduct();

		// -- create output
		OutputDirector director = null;
		OutputBuilder output = null;
		String outFileLow = outFile.toLowerCase();
		if (outFileLow.endsWith(".xls")) {
			output = new DefaultExcelOutputBuilder();
			director = new PushCellExcelOutputDirector(
					(ExcelOutputBuilder) output);
		} else if (outFileLow.endsWith(".html") || outFileLow.endsWith(".htm")) {
			output = new DefaultXhtmlOutputBuilder();
			director = new DefaultXhtmlOutputDirector((XhtmlOutputBuilder) output);
		} else {
			logger.info("Unsupport input sheet file type.\n");
			System.exit(0);
		}
		output.setModel(model);
		try {
			director.construct();
		} catch (XSheetParseException e) {
			logger.error("Construct out sheet error.\n" + e.getMessage());
			System.exit(1);
		}

		FileOutputStream fsout;
		try {
			fsout = new FileOutputStream(outFile);
			// export sheet
			output.write(fsout);
			fsout.close();
		} catch (IOException e) {
			logger.error("Occur IO error when creating output sheet file.\n"
					+ e.getMessage());
			System.exit(1);
		}
	}
}
